package com.ple.app.user;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import com.ple.app.jzsf.R;
import com.ple.app.login.LoginActivity;
import com.ple.app.util.SoilProperties;
import com.ple.app.util.UserInfo;

/**
 * Created by twenj on 2016/11/15.
 */

public class UserInfoActivity extends Activity {

    private Spinner spinnerFarms;
    private Button btnFarmInfoInput;
    private TextView txtFarmName;
    private TextView txtFarmID;
    private TextView txtFarmArea;
    private TextView txtFarmAddress;
    private TextView txtFarmSection;
    private Button btnUserExit;

    private ArrayAdapter<String> adapterFarms = null;
    private String[][] strFarmsInfo = null;

    private  UserInfo userInfo;

    private static final String USER_NAME = "username";
    private static final String PASSWORD = "password";
    private static final String ISSAVEPASS = "savePassword";
    private static final String AUTOLOGIN = "autoLogin";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_tabs);
        //初始化视图
        initView();
        //加载数据
        initData();
        //初始化事件
        initEvents();
    }

    private void initData() {
        strFarmsInfo = SoilProperties.FARMSINFO;//农场信息
        String[] farms = new String[]{"小汤山", "湖北"};

        adapterFarms = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, farms);
        spinnerFarms.setAdapter(adapterFarms);//数据绑定

    }

    private void initEvents() {
        //跳转到农场信息录入界面
        btnFarmInfoInput.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(UserInfoActivity.this, FarmsInfoInputActivity.class);
                startActivity(intent);
            }
        });

        //农场选择触发监听
        spinnerFarms.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0: {
                        bindValToTextView(strFarmsInfo[0]);
                        break;
                    }
                    case 1: {
                        bindValToTextView(strFarmsInfo[1]);
                        break;
                    }
                    default:
                        break;
                }
            }

            //绑定数据到视图
            private void bindValToTextView(String[] str) {
                txtFarmName.setText(str[0]);
                txtFarmID.setText(str[1]);
                txtFarmArea.setText(str[2]);
                txtFarmAddress.setText(str[3]);
                txtFarmSection.setText(str[4]);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        //注销用户登录
        btnUserExit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //注销时清空登录信息
                UserInfo userInfo = new UserInfo(UserInfoActivity.this);
                userInfo.clean();
                //跳转到登陆界面
                Intent intent = new Intent(UserInfoActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    private void initView() {

        spinnerFarms = (Spinner) findViewById(R.id.spinner_userInfo_farms);
        txtFarmName = (TextView) findViewById(R.id.txt_farm_name);
        txtFarmID = (TextView) findViewById(R.id.txt_farm_id);
        txtFarmArea = (TextView) findViewById(R.id.txt_farm_area);
        txtFarmAddress = (TextView) findViewById(R.id.txt_farm_address);
        txtFarmSection = (TextView) findViewById(R.id.txt_farm_section);
        //农场信息录入
        btnFarmInfoInput = (Button) findViewById(R.id.btn_userInfo_farmInfoInput);
        btnUserExit = (Button) findViewById(R.id.btn_userInfo_exit);
    }
}
